package com.jeesite.modules.app.util.wx;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class NioWebSocketServer {
       private final Logger logger=LoggerFactory.getLogger(this.getClass());
       public void init(){
    	   logger.info("正在启动websocket服务器");
    	   NioEventLoopGroup boss=new NioEventLoopGroup();
    	   NioEventLoopGroup work=new NioEventLoopGroup();
    	   try {
			ServerBootstrap bootstrap=new ServerBootstrap();
			bootstrap.group(boss,work);
			bootstrap.channel(NioServerSocketChannel.class);
			bootstrap.childHandler(new NioWebSocketChannelInitializer());
			Channel channel=bootstrap.bind(8888).sync().channel();
			logger.info("Websokcet服务器启动成功:"+channel);
			channel.closeFuture().sync();
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("运行错误:"+e);
		}finally{
			boss.shutdownGracefully();
			work.shutdownGracefully();
			logger.info("websocket服务器启动失败");
		}
       }
}
